/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package headquarters;

import com.vividsolutions.jts.geom.Point;
import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author marcin
 * odpowiada za wylosowanie aktualnej pozycji samochodu,
 * następnie pozycja ta jako jms jest wysyłana na server
 */
public class CarThread implements Runnable, Serializable {

    private static long numberIdGPS = 0; //unikalne dla sesji numer samochodu
//@Resource(mappedName="jms/ConnectionFactory")
//private  static ConnectionFactory connectionFactory;
//@Resource(mappedName="jms/Queue")
    private Queue queue;
    private Connection connection;
    private long idGps;
    EntityManagerFactory emf;
    int numberSteeet;

    public CarThread(Connection connection, Queue queue, EntityManagerFactory emf, int numberStreet) {
        idGps = numberIdGPS++;
        this.connection = connection;
        this.queue = queue;
        this.emf = emf;
        this.numberSteeet = numberStreet;

    }

    public void run() {


        Session session = null;
        MessageProducer messageProducer = null;
        TextMessage message = null;

        try {

            //  connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.SESSION_TRANSACTED);

            messageProducer = session.createProducer(queue);
            message = session.createTextMessage();

            Random random = new Random();
            Float x = random.nextFloat(); //losowanie położenia samochodu wsp.x
            Float y = random.nextFloat();//losowanie położenia samochodu wsp. y
            Calendar date = new GregorianCalendar();
            idGps = random.nextInt(100000);
            Float l; //dół [0,1]
            Float h; //góra [0,1] góra musi być większa od dołu

           City c = new City();
           boolean isCheangeStreet = false;
           int count = 0;

            while (true) {
                 System.out.println(numberSteeet+"to jest numer ulicy");
                x = random.nextFloat();
                 l = x;
                 h =new Float(1);
                Point p = new City().randomPoint(numberSteeet, l, h, emf);
                @SuppressWarnings("static-access")
                
                int hour = date.get(date.HOUR);
                if (date.get(date.AM_PM) == 1) hour+=12;
                String msg = idGps + " " + p.getX() + " " + p.getY() + " " +
                        date.get(date.YEAR) + " " + date.get(date.MONTH) + " " + date.get(date.DAY_OF_MONTH) + " " + hour + " " + date.get(date.MINUTE) + " " + date.get(date.SECOND);
                System.out.println(msg);
                message.setText(msg);

                messageProducer.send(message);
                TimeUnit.SECONDS.sleep(5);
                if (count >= 5) {
                    numberSteeet = random.nextInt(6);
                    numberSteeet = 5;
                    count = 0;
                }
                else
                count++;



            }

        } catch (InterruptedException ex) {
            Logger.getLogger(CarThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException e) {
            System.out.println("blad: " + e.toString());
        } finally {
            if (connection != null) {

                try {
                    connection.close();
                } catch (JMSException e) {
                    System.out.println("JMSException");
                }
            } // if

            System.exit(0);
        } // finally
    }
}
